package cz.data.domain.standard.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.DataConstant;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.standard.mapstruct.TypeMapper;
import cz.data.domain.standard.model.dto.TypeDto;
import cz.data.domain.standard.model.entity.TypeEntity;
import cz.data.domain.standard.model.query.TypeQuery;
import cz.data.domain.standard.model.vo.TypeVo;
import cz.data.domain.standard.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"标准类别信息表"})
@RestController
@RequestMapping("/types")
public class TypeController extends BaseController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private TypeMapper typeMapper;

    @ApiOperation(value = "标准类别信息表-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<TypeVo> getTypeById(@PathVariable String id) {
        TypeEntity typeEntity = typeService.getTypeById(id);
        return new SimpleDataResponse<>(typeMapper.toVO(typeEntity));
    }

    @ApiOperation(value = "标准类别信息表-获取列表")
    @GetMapping("/list")
    public TableDataResponse<TypeVo> getTypeList() {
        QueryWrapper<TypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", DataConstant.EnableState.ENABLE.getKey());
        List<TypeEntity> list = typeService.list(queryWrapper);
        List<TypeVo> collect = list.stream().map(typeMapper::toVO).collect(Collectors.toList());
        return new TableDataResponse<>(collect);
    }

    @ApiOperation(value = "标准类别信息表-分页查询")
    @ApiImplicitParam(name = "typeQuery", value = "查询实体typeQuery", required = true, dataTypeClass = TypeQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<TypeVo>> getTypePage(TypeQuery typeQuery) {
        QueryWrapper<TypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(typeQuery.getGbTypeCode()), "gb_type_code", typeQuery.getGbTypeCode());
        queryWrapper.like(StringUtils.isNotBlank(typeQuery.getGbTypeName()), "gb_type_name", typeQuery.getGbTypeName());
        IPage<TypeEntity> page = typeService.page(new Page<>(typeQuery.getPageNum(), typeQuery.getPageSize()), queryWrapper);
        List<TypeVo> collect = page.getRecords().stream().map(typeMapper::toVO).collect(Collectors.toList());
        JsonPage<TypeVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "标准类别信息表-新增")
    @ApiImplicitParam(name = "type", value = "详细实体type", required = true, dataType = "TypeDto")
    @PostMapping()
    public SimpleDataResponse<TypeVo> saveType(@RequestBody @Validated({ValidationGroups.Insert.class}) TypeDto type) {
        TypeEntity typeEntity = typeService.saveType(type);
        return new SimpleDataResponse<>(typeMapper.toVO(typeEntity));
    }

    @ApiOperation(value = "标准类别信息表-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "详细实体type", required = true, dataType = "TypeDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<TypeVo> updateType(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) TypeDto type) {
        TypeEntity typeEntity = typeService.updateType(type);
        return new SimpleDataResponse<>(typeMapper.toVO(typeEntity));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "标准类别信息表-删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteTypeById(@PathVariable String id) {
        typeService.deleteTypeById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "标准类别信息表-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteTypeBatch(@PathVariable List<String> ids) {
        typeService.deleteTypeBatch(ids);
        return BaseResponse.success();
    }
}
